define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'score_apply/index' + location.search,
                    add_url: 'score_apply/add',
                    edit_url: 'score_apply/edit',
                    del_url: 'score_apply/del',
                    multi_url: 'score_apply/multi',
                    import_url: 'score_apply/import',
                    table: 'score_apply',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'apply_code', title: __('Apply_code'), operate: 'LIKE',minWidth:170,formatter : function (value, row, index) {
                            return "<a href='javascript:void(0);' style='color: #4397fd' class='btn-dialog' " +
                                "data-url='score_apply/detail/ids/"+row.id+"' data-title='详情' data-area=[\"80%\",\"80%\"] >" + row.apply_code + "</a>";
                        }},
                        {field: 'admin_id', title: __('Admin_id'), formatter:Table.api.formatter.label,searchList:Config.adminlist},
                        {field: 'total_score', title: __('Total_score'), operate:'BETWEEN'},
                        {field: 'add_score', title: __('Add_score'), operate:'BETWEEN'},
                        {field: 'goodssales_code', title: __('Goodssales_code'), operate: 'LIKE',minWidth:170,formatter : function (value, row, index) {
                            return "<a href='javascript:void(0);' style='color: #4397fd' class='btn-dialog' " +
                                "data-url='kuerp/goods_sales/detail/ids/"+row.goodssales_id+"' data-title='订单详情' data-area=[\"80%\",\"80%\"] >" + row.goodssales_code + "</a>";
                        }},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'check_last_uid', title: __('最后审核人'), formatter: function(value, row, index){
                            return row.last_checkadmin && (row.last_checkadmin.nickname || '')
                        }, searchList:Config.examine_admins},
                        {field: 'check_status', title: __('Check_status'),operate: '=',searchList:Config.checkStatusList, formatter: Table.api.formatter.status},
                        {field: 'pay_status', title: __('Pay_status'), searchList:Config.payStatusList, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'score_apply/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'score_apply/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'score_apply/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        examinelist: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'score_apply/examinelist' + location.search,
                    table: 'score_apply_examine',
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        // {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'apply_code', title: __('Apply_code'), operate: 'LIKE',minWidth:170,formatter : function (value, row, index) {
                            return "<a href='javascript:void(0);' style='color: #4397fd' class='btn-dialog' " +
                                "data-url='score_apply/detail/ids/"+row.id+"' data-title='详情' data-area=[\"80%\",\"80%\"] >" + row.apply_code + "</a>";
                        }},
                        {field: 'admin_id', title: __('Admin_id'), formatter:Table.api.formatter.label,searchList:Config.adminlist},
                        {field: 'total_score', title: __('Total_score'), operate:'BETWEEN'},
                        {field: 'add_score', title: __('Add_score'), operate:'BETWEEN'},
                        {field: 'goodssales_code', title: __('Goodssales_code'), operate: 'LIKE',minWidth:170,formatter : function (value, row, index) {
                            return "<a href='javascript:void(0);' style='color: #4397fd' class='btn-dialog' " +
                                "data-url='kuerp/goods_sales/detail/ids/"+row.goodssales_id+"' data-title='订单详情' data-area=[\"80%\",\"80%\"] >" + row.goodssales_code + "</a>";
                        }},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'check_last_uid', title: __('最后审核人'), formatter: function(value, row, index){
                            return row.last_checkadmin && (row.last_checkadmin.nickname || '')
                        }, searchList:Config.examine_admins},
                        // {field: 'check_status', title: __('Check_status'), custom:{"0":"", "1":"info", "2":"success", "3":"danger"},searchList:Config.checkStatusList, formatter: Table.api.formatter.status},
                        {field: 'check_status', title: __('Check_status'), custom:{"0":"", "1":"info", "2":"success", "3":"danger"},searchList:{"0":__('Check_status 0'),"1":__('Check_status 1'),"2":__('Check_status 2'),"3":__('Check_status 3')},formatter:Table.api.formatter.status},
                        
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __(''),
                                    title: '详情',
                                    classname: 'btn btn-xs btn-info  btn-dialog',
                                    icon: 'fa fa-file-o',
                                    url: function (row, column) { //row 表格接收到的数据
                                        return "score_apply/detail?ids=" + row.id; //弹窗的对应后台控制器方法 这里是默认index方法 加id参数
                                    },
                                    extend: 'data-area=\'["90%","90%"]\'',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        detail: function () {
            //审核提交
            $(document).on('click', '.layer-footer .btn', function() {
                var type = $(this).data('value')
                console.log('type', type)
                if (type == 1) {
                    layer.confirm('确认审核通过？',{icon: 3, title:'提示'}, function(index){
                        //do something
                        examineSubmit(1)
                        layer.close(index);
                      });  
                } else {
                    var html='<div class="form-group">'+
                        '<label class="control-label col-xs-12 col-sm-3">审核备注：</label>'+
                        '<div class="col-xs-12 col-sm-9">'+
                        '<textarea class="form-control" name="remark" id="examine_remark" rows="5"></textarea>'+
                        '</div>'+
                       '</div>';
                    layer.open({
                        title: '审核不通过',
                        content: html,
                        area: ['50%', '50%'],
                        yes: function(index, layero){
                            var remark = $("#examine_remark").val();
                            if (!remark) {
                                layer.msg('请输入审核备注！')
                                return false
                            }
                            examineSubmit(0)
                            layer.close(index); //如果设定了yes回调，需进行手工关闭
                        }
                      }); 
                }
                return false
            })

            function examineSubmit(status) {
                var id = $("#ids").val();
                var remark = $("#examine_remark").val();
                //获取form所有input
                var formData = $('#examine-form').serialize()
                 // 添加额外的参数
                formData += '&id=' + id + '&remark=' + (remark ? encodeURIComponent(remark) : '') + '&status=' + status;
                $.ajax({
                    url: "score_apply/examine?ids="+id,
                    type: "POST",
                    data:formData,
                    success:function(res){
                        if(res.code==1){
                            reloadStatus = true
                            parent.location.reload();
                        }else{
                            layer.msg(res.msg, {icon: 2})
                        }
                        // layer.closeAll();
                    }
                })
            }
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
